//
//  XYCircularProgressView.m
//  ZhaoQiPei
//
//  Created by 找汽配 on 2017/2/13.
//  Copyright © 2017年 XiangYun. All rights reserved.
//

#import "XYCircularProgressView.h"

@interface XYCircularProgressView ()

//前面圆环
@property (nonatomic, strong) CAShapeLayer * foreCircle;
//背景圆环
@property (nonatomic, strong) CAShapeLayer * backCircle;

@end

@implementation XYCircularProgressView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self setupView];
}

- (void)setupView {
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.backCircle.frame = CGRectMake(self.bounds.size.width / 2 - self.radius,
                                       self.bounds.size.height / 2 - self.radius,
                                       self.radius * 2,
                                       self.radius * 2);
    self.foreCircle.frame = CGRectMake(self.bounds.size.width / 2 - self.radius,
                                       self.bounds.size.height / 2 - self.radius,
                                       self.radius * 2,
                                       self.radius * 2);
}

// 添加前景圆环
- (void)addForeCircleWithRadius:(CGFloat)radius lineWidth:(CGFloat)lineWidth {
    self.foreCircle = [self createShapeLayerWithRadius:radius lineWith:lineWidth startAngle:-M_PI_2 endAngle:M_PI_2 * 3 color:[UIColor whiteColor]];
    self.foreCircle.strokeStart = 0;
    self.foreCircle.strokeEnd = 0;
    [self.layer addSublayer:self.foreCircle];
}

//添加背景的圆环
- (void)addBackCircleWithRadius:(CGFloat)radius lineWidth:(CGFloat)lineWidth {
    self.backCircle = [self createShapeLayerWithRadius:radius lineWith:lineWidth startAngle:0 endAngle:M_PI * 2 color:[UIColor colorWithRed:80 / 255.0 green:80 / 255.0 blue:80 / 255.0 alpha:0.3]];
    self.backCircle.strokeStart = 0;
    self.backCircle.strokeEnd = 0;
    [self.layer addSublayer:self.backCircle];
}

// 创建圆环
- (CAShapeLayer *)createShapeLayerWithRadius:(CGFloat)radius lineWith:(CGFloat)lineWidth startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle color:(UIColor *)color {
    CAShapeLayer * layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(self.bounds.size.width / 2 - radius,
                             self.bounds.size.height / 2 - radius,
                             radius * 2,
                             radius * 2);
    UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius) radius:radius - lineWidth startAngle:startAngle endAngle:endAngle clockwise:YES];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.backgroundColor = [UIColor clearColor].CGColor;
    layer.lineCap = @"round";
    layer.lineWidth = lineWidth;
    layer.strokeColor = color.CGColor;
    return layer;
}

- (void)setRadius:(CGFloat)radius lineWidth:(CGFloat)lineWidth {
    self.radius = radius;
    self.lineWidth = lineWidth;
    if (self.backCircle) {
        [self.backCircle removeFromSuperlayer];
        self.backCircle = nil;
    }
    if (self.foreCircle) {
        [self.foreCircle removeFromSuperlayer];
        self.foreCircle = nil;
    }
    [self addBackCircleWithRadius:self.radius lineWidth:self.lineWidth];
    [self addForeCircleWithRadius:self.radius lineWidth:self.lineWidth];
}

- (void)setProgress:(CGFloat)progress {
    self.foreCircle.strokeEnd = progress;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
